﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;


namespace RoboRun
{
    
    public sealed partial class Tutorial2 : Page
    {
        private static BitmapImage[] bitmapArray = { null, null, null, null, null, null };
        private int count = 0;
        public Tutorial2()
        {
            this.InitializeComponent();
            float screenHeight = ScreenDimensions.obtainScreenHeight();
            float screenWidth = ScreenDimensions.obtainScreenWidth();
            addImageObjects();
            if (count == 0)
            {
                tutorialImage.Source = bitmapArray[count];
                disablePreviousButton();
            }
            loadPage(screenHeight, screenWidth);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void addImageObjects()
        {
            bitmapArray[0] = new BitmapImage() { UriSource = new Uri("ms-appx:///Assets/TutorialScreens/Tutorial2Screen/tutorial02_controlPanel.png", UriKind.Absolute) };
            bitmapArray[1] = new BitmapImage() { UriSource = new Uri("ms-appx:///Assets/TutorialScreens/Tutorial2Screen/tutorial02_commandQueue.png", UriKind.Absolute) };
            bitmapArray[2] = new BitmapImage() { UriSource = new Uri("ms-appx:///Assets/TutorialScreens/Tutorial2Screen/tutorial02_straight.png", UriKind.Absolute) };
            bitmapArray[3] = new BitmapImage() { UriSource = new Uri("ms-appx:///Assets/TutorialScreens/Tutorial2Screen/tutorial02_straightPrompt.png", UriKind.Absolute) };
            bitmapArray[4] = new BitmapImage() { UriSource = new Uri("ms-appx:///Assets/TutorialScreens/Tutorial2Screen/tutorial02_turnLeft.png", UriKind.Absolute) };
            bitmapArray[5] = new BitmapImage() { UriSource = new Uri("ms-appx:///Assets/TutorialScreens/Tutorial2Screen/tutorial02_turnRight.png", UriKind.Absolute) };
        }

        private void disablePreviousButton()
        {
            previousButton.Opacity = 0.5;
            previousButton.IsEnabled = false;
        }

        private void enablePreviousButton()
        {
            previousButton.Opacity = 1;
            previousButton.IsEnabled = true;
        }

        private void changeToNextButton()
        {
            nextButton.IsEnabled = true;
            nextButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
            nextTutorialButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            nextTutorialButton.IsEnabled = false;
        }

        private void changeToNextTutorialButton()
        {
            nextButton.IsEnabled = false;
            nextButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            nextTutorialButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
            nextTutorialButton.IsEnabled = true;
            nextTutorialButton.Content = "Tutorial 03: Controls";
        }

        private void loadPage(float screenHeight, float screenWidth)
        {
            //Position quit button
            quitButton.Margin = new Thickness(screenWidth / 100, screenHeight / 50, 0, 0);
            quitButton.Content = "Quit Tutorial";
            quitButton.Width = double.NaN;
            quitButton.Height = screenHeight / 20;

            //Position navigation buttons
            previousButton.Margin = new Thickness(screenWidth / 100, 0, 0, screenHeight / 50);
            previousButton.Height = screenHeight / 12;
            previousButton.Width = screenWidth / 20;
            nextButton.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right;
            nextButton.Margin = new Thickness(0, 0, screenWidth / 100, screenHeight / 50);
            nextButton.Height = screenHeight / 12;
            nextButton.Width = screenWidth / 20;

            //Format next tutorial button
            nextTutorialButton.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right;
            nextTutorialButton.Height = screenHeight / 10;
            nextTutorialButton.Width = screenWidth / 4.5;
            nextTutorialButton.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
            nextTutorialButton.Margin = new Thickness(0, 0, screenWidth / 50, 0);
            nextTutorialButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            nextTutorialButton.IsEnabled = false;
        }

        private void updatePage()
        {
            switch (count)
            {
                case 0:
                    disablePreviousButton();
                    changeToNextButton();
                    break;
                case 1:
                    enablePreviousButton();
                    changeToNextButton();
                    break;
                case 5:
                    changeToNextTutorialButton();
                    break;
                default:
                    enablePreviousButton();
                    changeToNextButton();
                    break;

            }
            tutorialImage.Source = bitmapArray[count];

        }

        private void previousButton_Click(object sender, RoutedEventArgs e)
        {
            count--;
            updatePage();
        }

        private void nextButton_Click(object sender, RoutedEventArgs e)
        {
            count++;
            updatePage();
        }

        private void quitButton_Click(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(TutorialSelectionPage));
        }

        private void nextTutorialButton_Click_1(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(Tutorial3));
        }
    }
}
